package cn.edu.njpi.service.impl;

import cn.edu.njpi.mapper.TClassMapper;
import cn.edu.njpi.mapper.TCollageMapper;
import cn.edu.njpi.mapper.TSchoolMapper;
import cn.edu.njpi.mapper.TUserMapper;
import cn.edu.njpi.model.TClass;
import cn.edu.njpi.model.TCollage;
import cn.edu.njpi.model.TSchool;
import cn.edu.njpi.model.TUser;
import cn.edu.njpi.service.TUserService;
import cn.edu.njpi.utils.InformUtils;
import cn.edu.njpi.utils.Sha256;
import cn.edu.njpi.utils.TokenTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.service.impl
 * @date 2022/4/19 7:00 PM
 */
@Service
public class TUserServiceImpl implements TUserService {
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TClassMapper tClassMapper;
    @Autowired
    private TCollageMapper tCollageMapper;
    @Autowired
    private TSchoolMapper tSchoolMapper;

    @Override
    public List<TUser> getAllUser() {
        List<TUser> tUserList = tUserMapper.getAllUser();
        changeUser(tUserList);
        tUserList.sort((o1, o2) -> {
            if (o1.getSchoolId() != null && o2.getSchoolId() != null) {
                return o1.getSchoolId() - o2.getSchoolId();
            }
            return 0;
        });
        return tUserList;
    }

    @Override
    public Object login(TUser tUser) {
        tUser.setPassword(Sha256.getSHA256(tUser.getPassword()));
        tUser = tUserMapper.selectUsernameAndPassword(tUser);
        if (tUser != null) {
            String t1 = (String) InformUtils.map.get(tUser.getUserId());
            if (t1 == null) {
                Date date = new Date(System.currentTimeMillis());
                tUser.setLastLoginTime(date);
                tUserMapper.updateByPrimaryKeySelective(tUser);
                tUser.setPassword(null);
                tUser.setUserPermssionName(InformUtils.getPermssionName(tUser.getUserPermssionId()));
                String token = TokenTool.getToken();
                InformUtils.map.put(token, tUser, 2, TimeUnit.HOURS);
                InformUtils.map.put(tUser.getUserId(), token, 2, TimeUnit.HOURS);
                Map<Object, Object> map = new HashMap<>();
                map.put("tUser", tUser);
                map.put("token", token);
                return map;
            } else {
                Map<Object, Object> map = new HashMap<>();
                map.put("tUser", InformUtils.map.get(t1));
                map.put("token", InformUtils.map.get(tUser.getUserId()));
                return map;
            }
        }
        return null;
    }

    @Override
    public List<TUser> selectUserBySchoolId(Integer schoolId) {
        List<TCollage> tCollages = tCollageMapper.selectByStudentId(schoolId);
        List<TUser> tUserList = new ArrayList<>();
        for (TCollage tCollage : tCollages) {
            List<TClass> tClasses = tClassMapper.selectClassByCollageId(tCollage.getCollageId());
            for (TClass tClass : tClasses) {
                List<TUser> tUserList1 = tUserMapper.selectUserByClassId(tClass.getClassId());
                tUserList.addAll(tUserList1);
            }
        }
        changeUser(tUserList);
        tUserList.sort((o1, o2) -> {
            if (o1.getCollageId() != null && o2.getCollageId() != null) {
                return o1.getCollageId() - o2.getCollageId();
            }
            return 0;
        });
        return tUserList;
    }

    @Override
    public List<TUser> selectUserByCollageId(Integer collageId) {
        List<TUser> tUserList = new ArrayList<>();
        List<TClass> tClasses = tClassMapper.selectClassByCollageId(collageId);
        for (TClass tClass : tClasses) {
            List<TUser> tUserList1 = tUserMapper.selectUserByClassId(tClass.getClassId());
            tUserList.addAll(tUserList1);
        }
        changeUser(tUserList);
        tUserList.sort((o1, o2) -> {
            if (o1.getClassId() != null && o2.getClassId() != null) {
                return o1.getClassId() - o2.getClassId();
            }
            return 0;
        });
        return tUserList;
    }

    @Override
    public List<TUser> selectUserByClassId(Integer classId) {
        List<TUser> tUserList = tUserMapper.selectUserByClassId(classId);
        changeUser(tUserList);
        tUserList.sort(Comparator.comparingInt(TUser::getCollageId));
        return tUserList;
    }

    private void changeUser(List<TUser> tUserList) {
        for (TUser tUser : tUserList) {
            tUser.setPassword(null);
            if (tUser.getClassId() != null) {
                TClass tClass = tClassMapper.selectByPrimaryKey(tUser.getClassId());
                tUser.setClassName(tClass.getClassName());
                TCollage tCollage = tCollageMapper.selectByPrimaryKey(tClass.getCollageId());
                tUser.setCollageId(tCollage.getCollageId());
                tUser.setCollageName(tCollage.getCollageName());
                TSchool tSchool = tSchoolMapper.selectByPrimaryKey(tCollage.getSchoolId());
                tUser.setSchoolId(tSchool.getSchoolId());
                tUser.setSchoolName(tSchool.getSchoolName());
            }
            tUser.setUserPermssionName(InformUtils.getPermssionName(tUser.getUserPermssionId()));
        }
    }
}
